//
//  HMMyWallet.swift
//  HealthMonitoring
//
//  Created by 珠海正杰 on 2018/9/14.
//  Copyright © 2018年 YTYanK. All rights reserved.
//

import UIKit

class HMMyWallet: HMViewController {
    
    let balance =  UILabel()
    let rmb = UILabel()
    let subView = UIView()
    
    private lazy var table: UITableView = {
        let _table = UITableView()
        return _table
    }()
    func setNavigationBarTranslucent() {
//        // 1.设置导航栏标题属性：设置标题颜色
       self.navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
//        // 2.设置导航栏前景色：设置item指示色
//       self.navigationController?.navigationBar.tintColor = UIColor.white
////        // 3.设置导航栏半透明
//       self.navigationController?.navigationBar.isTranslucent = true
////        // 4.设置导航栏背景图片
//       self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
//////        // 5.设置导航栏阴影图片
//       self.navigationController?.navigationBar.shadowImage = UIImage()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        setNavigationBarTranslucent()
    }
    
    /// 一个小组件
    func subNumberView(_ value: String, describe text: String) -> UIView {
       let title = UILabel()
       let describe = UILabel()
       let _subView = UIView()
       _subView.backgroundColor = UIColor.white
       _subView.addSubview(title)
       _subView.addSubview(describe)
        
       title.text = value
       title.topAlign(view: _subView, predicate: "0")
       title.leadingAlign(view: _subView, predicate: "0")
       title.trailingAlign(view: _subView, predicate: "0")
       title.height(view: _subView, predicate: "*0.5")
       title.textAlignment = .center
       
       describe.text = text
       describe.topVertically(view: title, predicate: "0")
       describe.leadingAlign(view: _subView, predicate: "0")
       describe.trailingAlign(view: _subView, predicate: "0")
       describe.bottomAlign(view: _subView, predicate: "0")
       describe.textAlignment = .center
        return _subView
    }
    
    func subIconView(_ value: String, iconImg: String) -> UIView {
        let icon = UIImageView()
        let describe = UILabel()
        let _subView = UIView()
        _subView.backgroundColor = UIColor.clear
        _subView.addSubview(icon)
        _subView.addSubview(describe)

        icon.imgName(name: iconImg)
        icon.centerYAlign(view: _subView, predicate: "-10")
        icon.centerXAlign(view: _subView, predicate: "0")
        icon.height(view: _subView, predicate: "*0.6")
        icon.width(view: _subView, predicate: "*0.4")
        
        describe.text = value
        describe.topVertically(view: icon, predicate: "0")
        describe.leadingAlign(view: _subView, predicate: "0")
        describe.trailingAlign(view: _subView, predicate: "0")
        describe.bottomAlign(view: _subView, predicate: "0")
        describe.textAlignment = .center
//        describe.backgroundColor = UIColor.blue
        return _subView
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
//        self.navigationController?.navigationBar.isTranslucent = false
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black]
//        self.navigationController?.navigationBar.tintColor = configure.shareConfigure.HMColor1
//
//        //重置导航栏背景
//        self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
//        self.navigationController?.navigationBar.shadowImage = nil
    }

    
    override func setupView() {
//        self.title = "我的钱包"
        view.backgroundColor = configure.shareConfigure.HMColor1
        
        view.addSubview(balance)
        view.addSubview(rmb)
        view.addSubview(subView)
        view.addSubview(table)
       
        let itemL = subNumberView("20人", describe: "我推荐的好友")
        let itemR = subNumberView("20人", describe: "好友的朋友")
        
        let buy = subIconView("购买设备", iconImg: "buyDev")
        let recharge = subIconView("充值", iconImg: "recharge")
        let put = subIconView("提现", iconImg: "put")
        view.addSubview(buy)
        view.addSubview(recharge)
        view.addSubview(put)
        view.addSubview(itemL)
        view.addSubview(itemR)
        
        balance.text = "我的余额:"
        balance.textColor = UIColor.white
        balance.topAlign(view: view)
        balance.leadingAlign(view: view, predicate: "25")
        balance.trailingAlign(view: view)
        balance.heightConstrain(predicate: "44")
//        balance.backgroundColor = .blue
    
        
        rmb.text = "60.0元"
        rmb.textColor = UIColor.white
        rmb.topVertically(view: balance)
        rmb.leadingAlign(view: view)
        rmb.trailingAlign(view: view)
        rmb.heightConstrain(predicate: "120")
        rmb.font = UIFont.systemFont(ofSize: 35)
//        rmb.backgroundColor = .red
        rmb.textAlignment = .center
        
        
        
        let itemW :CGFloat = (configure.shareConfigure.screenWidth - 16)/3
        
        buy.topVertically(view: rmb, predicate: "0")
        buy.leadingAlign(view: view, predicate: "8")
        buy.heightConstrain(predicate: "80")
        buy.widthConstrain(predicate: "\(itemW)")
        
        recharge.topVertically(view: rmb, predicate: "0")
        recharge.leadingHorizontally(view: buy, predicate: "0")
        recharge.heightConstrain(predicate: "80")
        recharge.widthConstrain(predicate: "\(itemW)")
        
        put.topVertically(view: rmb, predicate: "0")
        put.leadingHorizontally(view: recharge, predicate: "0")
        put.heightConstrain(predicate: "80")
        put.widthConstrain(predicate: "\(itemW)")
        
        itemL.topVertically(view: buy,predicate: "10")
        itemL.leadingAlign(view: view, predicate: "0")
        itemL.heightConstrain(predicate: "50")
        itemL.width(view: view, predicate: "*0.5")
        
        itemR.topVertically(view: buy, predicate: "10")
        itemR.leadingHorizontally(view: itemL, predicate: "2")
        itemR.heightConstrain(predicate: "50")
        itemR.width(view: view, predicate: "*0.5")
        
        table.delegate = self
        table.dataSource = self
        table.topVertically(view: itemL, predicate: "\(2)")
        let h = view.wHeight - 250
        table.heightConstrain(predicate: "\(h)")
        table.leadingAlign(view: view, predicate: "0")
        table.trailingAlign(view: view, predicate: "0")
        table.register(HMSessionCell.classForCoder(), forCellReuseIdentifier: "HMSessionCell")
    }
    /// 添加导航栏右 按钮
    func addRBtn() {
        let but = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 38))
        but.setTitle("推荐规则", for: .normal)
        but.setTitleColor(configure.shareConfigure.HMColor1, for: .normal)
        but.addTarget(self, action: #selector(seeRules), for: UIControlEvents.touchUpInside)
        let itemR = UIBarButtonItem.init(customView: but)
        self.navigationItem.rightBarButtonItem = itemR
    }
    
    @objc func seeRules(sender: UIButton) {
        print("seeRules----")
    }
}

extension HMMyWallet : UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return  88
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = table.dequeueReusableCell(withIdentifier: "HMSessionCell") as! HMSessionCell
        cell.delegate = self
        return cell
    }
    
    
}

extension HMMyWallet: HMSessionCellDelegate {
    func sessionCellBtn(sender: UIButton, doctor: Bool) {
        print("sender------->\(sender)")
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let _im : InstantMessaging = storyBoard.instantiateViewController(withIdentifier: "IM") as! InstantMessaging
        self.pushViewController(_im)
    }
    
    
}
